<template>
  <div class="clas_All">
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-dark">
          <el-form :model="AllotList" size="small" :inline="true">
            <el-form-item prop="" style="height: 40px">
              <el-date-picker
                clearable
                v-model="AllotList.paramTime"
                type="month"
                placeholder="选择月"
                @change="createdTimeChange"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item prop="">
              <el-select
                v-model="AllotList.deptCode"
                placeholder="请选择部门"
                clearable
                @change="deptChange"
              >
                <el-option
                  v-for="dict in deptOptions"
                  :key="dict.relateId"
                  :label="dict.label"
                  :value="dict.relateId"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="mini" @click="getBase"
                >查询</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col :span="24">
        <el-table :data="tableData" :row-click="handleRowClick">
          <el-table-column
            align="center"
            prop="month"
            label="月份"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="starttime"
            label="开始时间"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="endtime"
            label="结束时间"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="deptName"
            label="店铺名称"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="pandianCode"
            label="盘点编码"
          ></el-table-column>
          <el-table-column align="center" prop="flag" label="状态">
            <template slot-scope="scope">
              <div v-if="scope.row.flag">已关账</div>
              <div v-else>未关账</div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <div>
                <el-button
                  size="mini"
                  type="text"
                  @click="handleLook(scope.row)"
                  v-hasPermi="['pandian:pandianPeriod:guanzhang']"
                >
                  关账
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="AllotList.pageNum"
          :limit.sync="AllotList.pageSize"
          @pagination="getBase"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getpandianPeriod, closeBill } from "@/api/system/information";
import { getPanDianCode, FindShopBom } from "@/api/system/stock";
export default {
  data() {
    return {
      total: 0,
      AllotList: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      deptOptions: [],
      byMonth: false,
    };
  },
  methods: {
    getBase() {
      if (this.AllotList.deptCode != null) {
        getpandianPeriod({ ...this.AllotList }).then((res) => {
          if (res.code == 200) {
            this.tableData = res.rows;
            this.total = res.total;
            this.tableData.forEach((ite) => {
              // ite.month =
              let data = new Date(ite.endtime);
              let year = data.getFullYear();
              let month = data.getMonth() + 1; // 月份是从 0 开始计数的，所以要加 1
              let day = data.getDate();
              ite.endtime = year + "-" + month + "-" + day;
              let data1 = new Date(ite.starttime);
              let year1 = data1.getFullYear();
              let month1 = data1.getMonth() + 1; // 月份是从 0 开始计数的，所以要加 1
              let day1 = data1.getDate();
              ite.starttime = year1 + "-" + month1 + "-" + day1;
            });
          }
          // console.log(res);
        });
      } else {
        this.$message({
          message: "请选择部门",
          type: "warning",
          center: true,
        });
      }
    },
    handleRowClick() {},
    handleLook(v) {
      closeBill(v.id).then((res) => {
        if (res.code == 200) {
          // console.log(res);
          this.$message({
            type: "info",
            message: "关账成功",
          });
          this.getBase();
        } else {
          this.$message({
            type: "info",
            message: "已取消",
          });
        }
      });
    },
    FindShop() {
      FindShopBom().then((res) => {
        // console.log(res);
        if (res.code == 200) {
          let ovj = res.data.filter((ite) => {
            if (ite.relateId !== null) {
              delete ite.children;
              return ite;
            }
          });
          if (ovj.length > 1) {
            this.mouday = true;
          }
          this.deptOptions = ovj;
        }
      });
    },
    createdTimeChange(v) {
      let data = new Date(v);
      let y = data.getFullYear();
      let m = data.getMonth() + 1;
      this.AllotList.paramTime = y + String(m).padStart(2, "0");
    },
    deptChange(v) {
      // console.log(v);
      if (v) {
        this.mouday = false;
      } else {
        this.mouday = true;
      }
    },
  },
  created() {
    // this.getBase();
    this.FindShop();
  },
};
</script>

<style lang="scss" scoped>
.clas_All {
  margin: 20px;
}
</style>
